/**
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2010  Sean Beecroft

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

 * @version 1.10 2010-01-01
 * @author Sean Beecroft, Various (some undocumented)
 */
package yawiki.server.util;

public class StringUtility {

    public static String capitalize(String in) {
        if (in.length() == 0) {
            return "";
        } else {
            char c = in.charAt(0);
            String ret = "" + c;

            ret = ret.toUpperCase();
            ret = ret + in.substring(1, in.length());
            return ret;
        }
    }

    /**
     * If Java 1.4 is unavailable, the following technique may be used.
     *
     * @param aInput is the original String which may contain substring aOldPattern
     * @param aOldPattern is the non-empty substring which is to be replaced
     * @param aNewPattern is the replacement for aOldPattern
     */
    public static String replace(
            final String aInput,
            final String aOldPattern,
            final String aNewPattern) {
        if (aOldPattern.equals("")) {
            throw new IllegalArgumentException("Old pattern must have content.");
        }

        final StringBuffer result = new StringBuffer();
        //startIdx and idxOld delimit various chunks of aInput; these
        //chunks always end where aOldPattern begins
        int startIdx = 0;
        int idxOld = 0;
        while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
            //grab a part of aInput which does not include aOldPattern
            result.append(aInput.substring(startIdx, idxOld));
            //add aNewPattern to take place of aOldPattern
            result.append(aNewPattern);

            //reset the startIdx to just after the current match, to see
            //if there are any further matches
            startIdx = idxOld + aOldPattern.length();
        }
        //the final chunk will go to the end of aInput
        result.append(aInput.substring(startIdx));
        return result.toString();
    }

    public static int indexOfIgnoreCase(String strA, String strB) {
        String testA = strA.toLowerCase();
        String testB = strB.toLowerCase();
        int ptr = testA.indexOf(testB);
        return ptr;
    }

    public static boolean isAlphaNumeric(char c) {
        boolean blnNumeric = false;
        boolean blnAlpha = false;

        if (c >= '0' && c <= '9') {
            return true;
        }

        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
            return true;
        }
        return false;
    }

    public static boolean isCharacter(char c) {

        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
            return true;
        }
        return false;
    }

    public static boolean isNumber(char c) {

        if (c >= '0' && c <= '9') {
            return true;
        }
        return false;
    }
}
